package com.dp.masterwoker;

import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;

public abstract class Worker implements Runnable {
    //任务队列，用于取得子任务
    protected Queue<Object> workQueue;
    //子任务处理结果集
    protected Map<String, Object> resultMap;
    
    public void setWorkQueue(Queue<Object> workQueue){
        this.workQueue = workQueue;
    }
    
    public void setResultMap(Map<String, Object> resultMap){
        this.resultMap = resultMap;
    }
    
    /**
     * 处理输入的方法
     * @param input
     * @return
     */
    public abstract Object handle(Object input);
    
    @Override
    public void run(){
        while(true){
            //获取子任务
            Object input = workQueue.poll();
            if (input == null) {
                break;
            }
            //处理子任务
            Object re = handle(input);
            //将处理结果写入结果集
            resultMap.put(Integer.toString(input.hashCode()), re);
        }
    }
}
